﻿using Autofac.Integration.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http.Filters;
using Wechat.Util.Log4Net;
using Wechat.Util.ReturnMessage;
using Wechat.Util.StateCode;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Wechat.API.Filter
{
    /// <summary>
    /// 异常过滤器
    /// </summary>
    public class ExceptionHandleActionFilter : IAutofacExceptionFilter
    {
        public Task OnExceptionAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest,
                new ResultMessage(
                    false,
                    (int)EnumApiStateCode.SystemError,
                    "Sorry,服务器开了小差！"));

            LogHelper.WriteLog(typeof(ExceptionHandleActionFilter), actionExecutedContext.Exception.StackTrace);
            return Task.FromResult(0);
        }
    }
}